package com.beemans.corekit.ui.fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.beemans.corekit.constant.AdvertConstants;
import com.beemans.corekit.databinding.FragmentAdmobSplashBinding;
import com.beemans.corekit.integration.admob.ATAdInfoExt;
import com.beemans.corekit.integration.admob.AbAdvertManager;
import com.beemans.corekit.integration.admob.AbSplashAdvert;
import com.beemans.corekit.integration.admob.AbSplashAdvertCallback;
import com.beemans.corekit.integration.admob.AbSplashAdvertConfig;
import com.beemans.corekit.integration.toolkit.ToastUtils;
import com.beemans.corekit.ui.dialog.LoadingDialog;
import com.google.android.gms.ads.AdValue;
import com.google.android.gms.ads.ResponseInfo;

public class AdmobSplashFragment extends BaseFragment<FragmentAdmobSplashBinding> {
    @Nullable
    @Override
    protected FragmentAdmobSplashBinding getViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
        return FragmentAdmobSplashBinding.inflate(inflater, container, false);
    }

    @Override
    protected void initViews() {
    }

    @Override
    protected void initEvents() {
        binding.splashBtnLoadAd.setOnClickListener(v -> preloadAdvert());

        binding.splashBtnShowAd.setOnClickListener(v -> showAdvert());
    }

    private void preloadAdvert() {
        LoadingDialog.showLoading();

        final AbSplashAdvertConfig advertConfig = new AbSplashAdvertConfig(AdvertConstants.AB_SPLASH_ID);
        AbAdvertManager.loadSplashAdvert(this, advertConfig, getAdvertCallback());
    }

    private void showAdvert() {
        LoadingDialog.showLoading();

        final AbSplashAdvertConfig advertConfig = new AbSplashAdvertConfig(AdvertConstants.AB_SPLASH_ID);
        advertConfig.setShowAfterLoaded(true);
        advertConfig.setPreloadAdvert(true);

        AbAdvertManager.loadSplashAdvert(this, advertConfig, getAdvertCallback());
    }

    @NonNull
    private AbSplashAdvertCallback getAdvertCallback() {
        return new AbSplashAdvertCallback() {
            @Override
            public void onAdvertRequestPre(@NonNull AbSplashAdvert abSplashAdvert, @NonNull ATAdInfoExt adInfoExt) {
                printLog("广告开始请求");
            }

            @Override
            public void onAdvertRequestAlt(@NonNull AbSplashAdvert abSplashAdvert, @NonNull ATAdInfoExt adInfoExt) {
                printLog("广告请求成功");
            }

            @Override
            public void onAdvertLoaded(@NonNull AbSplashAdvert abSplashAdvert) {
                printLog("广告加载成功");
                LoadingDialog.hideLoading();
            }

            @Override
            public void onAdvertLoadFail(@NonNull AbSplashAdvert abSplashAdvert, @Nullable String adError) {
                printLog("广告加载失败");
                LoadingDialog.hideLoading();
            }

            @Override
            public void onAdvertShow(@NonNull AbSplashAdvert abSplashAdvert, @Nullable ResponseInfo responseInfo) {
                printLog("广告展示 --- responseInfo:" + responseInfo);
            }

            @Override
            public void onAdvertShowFail(@NonNull AbSplashAdvert abSplashAdvert) {
                printLog("广告展示失败");
            }

            @Override
            public void onAdvertClicked(@NonNull AbSplashAdvert abSplashAdvert, @Nullable ResponseInfo responseInfo) {
                printLog("广告点击 --- responseInfo:" + responseInfo);
            }

            @Override
            public void onAdvertClose(@NonNull AbSplashAdvert abSplashAdvert, @Nullable ResponseInfo responseInfo) {
                printLog("广告关闭 --- responseInfo:" + responseInfo);
            }

            @Override
            public void onAdvertRevenue(@NonNull AbSplashAdvert abSplashAdvert, @Nullable ResponseInfo responseInfo, @NonNull AdValue adValue) {
                printLog("广告收益 --- getValueMicros:" + adValue.getValueMicros());
                printLog("广告收益精度 --- getPrecisionType:" + adValue.getPrecisionType());
                printLog("广告收益货币代码 --- getCurrencyCode:" + adValue.getCurrencyCode());
            }
        };
    }

    private void printLog(String msg) {
        Log.e("susu", msg);
        ToastUtils.showShort(msg);
    }
}